<template>
  <el-row>
    <edit-modal ref="editModal" @refresh="refresh"></edit-modal>
    <el-col :span="24">
      <Portlet>
        <el-form ref="searchForm" class="el-form-col search-form" :model="search">
          <el-row :gutter="15">

            <el-col :lg="4" :md="6">
              <el-form-item prop="pickingName">
                <el-input v-model="search.pickingName" placeholder="拣货员姓名"></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="logAccount">
                <el-input v-model="search.logAccount" placeholder="拣货员电话" clearable></el-input>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6">
              <el-form-item prop="pickingType">
                <el-select v-model="search.pickingType" placeholder="拣货类型">
                  <el-option v-for="item in pickingTypeArr"
                             :key="item.value"
                             :label="item.text"
                             :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-lg-only hidden-xl-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="8">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>
                  </el-col>
                  <el-col :span="8">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>

            <el-col :lg="4" :md="6" class="hidden-md-only hidden-sm-only">
              <el-form-item>
                <el-row class="search-btns" :gutter="5">
                  <el-col :span="12">
                    <el-button type="primary" :loading="searchLoading" plain native-type="submit"
                               @click.prevent="searchFunc(search)"><span>搜索</span></el-button>
                  </el-col>
                  <!--<el-col :span="8">-->
                  <!--<el-button type="info" plain @click="isOpen = !isOpen">{{ isOpen ? '收起' : '展开' }}</el-button>-->
                  <!--</el-col>-->
                  <el-col :span="12">
                    <el-button @click="resetFields" plain>重置</el-button>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </Portlet>
    </el-col>
    <el-col :span="24">
      <BasicTable
        ref="BasicTable"
        url="/ms-warehouse-order/resourceManage/selectPicking"
        :tabs="tabs"
        :columns="columns">
        <template slot="buttons">
          <el-button type="primary" @click="add">新增</el-button>
        </template>
        <template slot="column">
          <el-table-column fixed="right" width="100px" align="center" label="操作">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="editRowId(scope.row)"
              >
                修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="delRowId(scope.row)"
              >
                移除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </BasicTable>
    </el-col>
  </el-row>
</template>
<script>
import EditModal from '@/views/oms/resource/pick/EditModal'
import {filterDataBaseDictionary, unixTimestampFilter} from '@/filters'
import {copy, confirm} from '@/utils'
import {getSubDictListOfParentCode} from '@/utils/selectOptions'
export default {
  name: 'resourcePick',
  components: {
    EditModal
  },
  data() {
    let columns = [
      {
        label: '拣货员姓名',
        data: 'name'
      },
      {
        label: '拣货员电话',
        data: 'phone'
      },
      {
        label: '拣货类型',
        data: 'pickingType',
        render(data, full) {
          return filterDataBaseDictionary(data)
        }
      },
      {
        label: '添加时间',
        data: 'foundTime',
        render(data, full) {
          return unixTimestampFilter(data)
        }
      }
    ]
    return {
      pickingTypeArr: [], // 车辆类型
      isOpen: false,
      visible: false,
      searchLoading: false,
      search: {
        logAccount: '',
        pickingName: '',
        pickingType: ''
      },
      columns,
      tabs: []
    }
  },
  created() {
    getSubDictListOfParentCode('PICKUPTYPE').then(arr => {
      this.pickingTypeArr = arr
    })
  },
  methods: {
    // 搜索
    searchFunc(search) {
      const basicTable = this.$refs.BasicTable
      if (basicTable) {
        let params = copy(search)
        this.searchLoading = true
        basicTable.search(params).finally(_ => {
          this.searchLoading = false
        })
      }
    },
    // 重置表单
    resetFields() {
      const searchForm = this.$refs.searchForm
      if (searchForm) {
        searchForm.resetFields()
      }
    },
    // 表格刷新
    refresh() {
      this.searchFunc(this.search)
    },
    add() {
      this.$refs.editModal.show()
    },
    editRowId(data) {
      this.$refs.editModal.show(data)
    },
    delRowId(data) {
      console.log('delete')
      confirm('你确定要从拣货资源列表中移除该拣货员吗？', '温馨提示', {
        confirmButtonLoading: true,
        type: 'waring',
        confirmCallBack: ({confirmButtonLoadingClose, close}) => {
          console.log('confirmCallBack')
          return this.$ajax.get('/ms-warehouse-order/resourceManage/deletePicking', {id: data.id}).then(response => {
            close()
            if (response.success) {
              this.refresh()
              this.$message({
                type: 'success',
                message: '移除成功!'
              })
            }
          }).finally(_ => {
            confirmButtonLoadingClose()
          })
        }
      })
    }
  }
}
</script>
